package com.xiaomili.halfsugar.fragment;


import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.google.gson.Gson;
import com.xiaomili.halfsugar.R;
import com.xiaomili.halfsugar.bean.ProductBean;
import com.xiaomili.halfsugar.constant.UrlString;
import com.xiaomili.halfsugar.utils.OKHttpUtils;


import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * 搜索页面---GridView碎片
 * A simple {@link Fragment} subclass.
 */
public class SearchProductFragment extends Fragment {
    private ProductBean productBean;
    private RadioGroup mRadioGroup;
    private LinearLayout mLinearLayout;
    private int mCurrentIndex, mPreviousIndex;
    private List<Fragment> mFragmentList;

    public SearchProductFragment() {
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_search_product, container, false);
        return view;
    }

    private void initFragment(ProductBean productBean) {
        mFragmentList = new ArrayList<>();
        for (int i = 0; i < productBean.data.size(); i++) {
            mFragmentList.add(SearchProductGridViewFragment.newSearchProductGridViewFragment(UrlString.PRODUCT_URL, i));
        }
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        mRadioGroup = (RadioGroup) view.findViewById(R.id.search_product_rg);
        mLinearLayout = (LinearLayout) view.findViewById(R.id.search_product_ll);
        Log.i("result","URL----------->"+UrlString.PRODUCT_URL);
        initRadioButton();//网络加载设置单项按钮
        mRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                RadioButton radioButton = (RadioButton) group.findViewById(checkedId);
                mCurrentIndex = group.indexOfChild(radioButton);
                if (mCurrentIndex != mPreviousIndex) {
                    RadioButton rb = (RadioButton) group.getChildAt(mPreviousIndex);
                    rb.setBackgroundColor(getResources().getColor(R.color.rb_false));
                    show_HideFragment(mCurrentIndex, mPreviousIndex);
                }
                radioButton.setBackgroundColor(getResources().getColor(R.color.rb_true));
                mPreviousIndex = mCurrentIndex;
            }
        });
    }

    //网络加载设置单项按钮
    private void initRadioButton() {
        OKHttpUtils.newInstance(getContext()).obtainDataAsyncByGet(UrlString.PRODUCT_URL, new OKHttpUtils.MyCallback() {

            @Override
            public void onFailure(IOException e) {

            }

            @Override
            public void onResponse(String result) {
                boolean one = true;//第一个设置被点中的
                Log.i("result","----------->"+result);
                productBean = new Gson().fromJson(result, ProductBean.class);
                for (int i = 0; i < productBean.data.size(); i++) {
                    RadioButton radioButton = new RadioButton(getContext());
                    radioButton.setLayoutParams(new RadioGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 90));
                    radioButton.setGravity(Gravity.CENTER);
                    radioButton.setId(i);
                    //去除RadioButton的圆点
                    Bitmap a = null;
                    radioButton.setButtonDrawable(new BitmapDrawable(a));
                    radioButton.setText(productBean.data.get(i).name);
                    radioButton.setTextColor(getResources().getColorStateList(R.color.search_product_radiobutton_text));
                    radioButton.setChecked(false);
                    radioButton.setBackgroundColor(getResources().getColor(R.color.rb_false));
                    mRadioGroup.addView(radioButton);
                }

                RadioButton rb = (RadioButton) mRadioGroup.getChildAt(mCurrentIndex);
                rb.setChecked(true);
                rb.setBackgroundColor(getResources().getColor(R.color.rb_true));
                initFragment(productBean);
                show_HideFragment(mCurrentIndex, mPreviousIndex);
            }
        });
    }

    //显示隐藏单品中GridView碎片
    private void show_HideFragment(int current, int previous) {
        FragmentTransaction fragmentTransaction = getChildFragmentManager().beginTransaction();
        Fragment fragment = mFragmentList.get(current);
        if (!fragment.isAdded()) {
            fragmentTransaction.add(R.id.search_product_ll, fragment);
        }
        if (mCurrentIndex == mPreviousIndex) {
            fragmentTransaction.show(fragment);
        } else {
            fragmentTransaction.show(fragment).hide(mFragmentList.get(previous));
        }
        fragmentTransaction.commit();
    }
}
